import { login } from "@/api/goods";
// import { getToken, setToken, removeToken } from '@/utils/auth'
import { userZhuCe } from "@/api/goods.js";
const dianUser = {
  state: {
    token: "",
    dianName: "",
    dianUserId: "",
    phone: "",
    email:"",
    avatar: "",
    roles: [],
    permissions: [],
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_DIAN_NAME: (state, name) => {
      state.dianName = name;
    },
    SET_DIAN_USER_ID: (state, id) => {
      state.dianUserId = id;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    SET_PHONE: (state, phone) => {
      state.phone = phone;
    },
    SET_EMAIL: (state, email) => {
      state.email = email;
    }
  },

  actions: {
    userRegister({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        userZhuCe({
          username: userInfo.username,
          password: userInfo.password,
        })
          .then((res) => {
            this.loading = false;
            if (res.code == 200) {
              console.log("1212312", res);
              commit("SET_DIAN_NAME", userInfo.username);
              commit("SET_DIAN_USER_ID", res.userInfo.id);
              commit("SET_AVATAR", "../../assets/images/default.jpeg");
              commit("SET_TOKEN", res.token);
              sessionStorage.setItem("userToken", res.token);
              resolve();
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 登录
    dianLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo)
          .then((res) => {
            console.log('res :>> ', res);
            commit("SET_DIAN_NAME", userInfo.username);
            commit("SET_DIAN_USER_ID", res.userInfo.id);
            commit("SET_PHONE", res.userInfo.phone);
            commit("SET_EMAIL", res.userInfo.email);
            commit("SET_AVATAR", "../../assets/images/default.jpeg");
            commit("SET_TOKEN", res.token);
            sessionStorage.setItem("userToken", res.token);
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 获取用户信息
    // GetInfo({ commit, state }) {
    //   return new Promise((resolve, reject) => {
    //     getInfo()
    //       .then((res) => {
    //         const user = res.user;
    //         const avatar =
    //           user.avatar == "" || user.avatar == null
    //             ? require("@/assets/images/profile.png")
    //             : process.env.VUE_APP_BASE_API + user.avatar;
    //         if (res.roles && res.roles.length > 0) {
    //           // 验证返回的roles是否是一个非空数组
    //           commit("SET_ROLES", res.roles);
    //           commit("SET_PERMISSIONS", res.permissions);
    //         } else {
    //           commit("SET_ROLES", ["ROLE_DEFAULT"]);
    //         }
    //         commit("SET_DIAN_NAME", user.userName);
    //         commit("SET_AVATAR", avatar);
    //         resolve(res);
    //       })
    //       .catch((error) => {
    //         reject(error);
    //       });
    //   });
    // },

    // 退出系统
    DianLogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        commit("SET_TOKEN", "");
        sessionStorage.clear();
        commit("SET_DIAN_NAME", "");
        commit("SET_AVATAR", "");
        commit("SET_DIAN_USER_ID", "");
        commit("SET_PHONE", "");
        commit("SET_EMAIL", "");
       
        // sessionStorage.setItem("userToken", res.token);
        resolve();
      });
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise((resolve) => {
        commit("SET_TOKEN", "");
        removeToken();
        resolve();
      });
    },

    // 设置手机号
    setPhone({ commit },phone) {
      return new Promise((resolve) => {
        commit("SET_PHONE", phone);
        resolve();
      });
    },


    // 设置邮箱
    setEmail({ commit },email) {
      return new Promise((resolve) => {
        commit("SET_EMAIL", email);
        resolve();
      });
    },
  },
};

export default dianUser;
